﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using System.IO;
using EnvDTE80;
using System.Reflection;

namespace QuickMAP.Code
{
    public class Actions
    {
        public const string HOST_URL = "hosturl";

        public static DTE DTEObject = null;
        /// <summary>
        /// 添加模板到项目
        /// </summary>
        /// <param name="tplPath"></param>
        public static void AppendTemplate(string tplPath)
        {
            EnvDTE.TextSelection docSel = DTEObject.ActiveDocument.Selection as TextSelection;

            StringBuilder allContent = new StringBuilder();
            string[] contents = File.ReadAllLines(tplPath, Encoding.UTF8);
            docSel.NewLine(1);
            int startLine = docSel.ActivePoint.Line;
            int startOffset = docSel.ActivePoint.LineCharOffset;
            int endLine = 0;
            int endOffset = 0;
            foreach (string line in contents)
            {
                //## 为注释行
                if (line.StartsWith("##"))
                {
                    continue;
                }
                allContent.Append(line).Append("\\n");
            }

            docSel.Text = " ";
            docSel.MoveToLineAndOffset(docSel.ActivePoint.Line, docSel.ActivePoint.LineCharOffset - 1, true);
            //注释掉以前一行行的写入方式，改用replace方式，不会出现添加模板的时候，一行行写入很慢的情况。
            docSel.ReplaceText(" ", allContent.ToString(), (int)vsFindOptions.vsFindOptionsRegularExpression);

            ////写入模板
            //foreach (string line in contents)
            //{
            //    //## 为注释行
            //    if (line.StartsWith("##"))
            //    {
            //        continue;
            //    }
            //    var tmpLine = line;
            //    tmpLine = tmpLine.Replace("<", "{<<<<<}");
            //    tmpLine = tmpLine.Replace(">", "{>>>>>}");
            //    tmpLine = tmpLine.Replace("=", "{=====}");
            //    docSel.Text = tmpLine;
            //    docSel.NewLine(1);
            //    docSel.MoveToLineAndOffset(docSel.ActivePoint.Line, startOffset, false);
            //}

            ////记录结束标记
            //endLine = docSel.ActivePoint.Line;
            //endOffset = docSel.ActivePoint.LineCharOffset;

            ////光标移动到新位置
            //docSel.MoveToLineAndOffset(startLine, startOffset, true);
            //docSel.ReplaceText("{<<<<<}", "<", (int)vsFindOptions.vsFindOptionsFromStart);
            //docSel.MoveToLineAndOffset(endLine, endOffset, false);
            //docSel.MoveToLineAndOffset(startLine, startOffset, true);
            //docSel.ReplaceText("{>>>>>}", ">", (int)vsFindOptions.vsFindOptionsFromStart);
            //docSel.MoveToLineAndOffset(endLine, endOffset, false);
            //docSel.MoveToLineAndOffset(startLine, startOffset, true);
            //docSel.ReplaceText("{=====}", "=", (int)vsFindOptions.vsFindOptionsFromStart);
            //docSel.MoveToLineAndOffset(endLine, endOffset, false);
        }

        /// <summary>
        /// 打开帮助文件路径
        /// 支持url地址的帮助文件，帮助文件会从当前目录开始向上查找一个HostURL地址，然后追加当前_hlp文件配置的路径地址成为一个完整的URL地址，从该地址打开帮助文档
        /// </summary>
        /// <param name="hlpPath"></param>
        public static void OpenHelp(string hlpPath)
        {
            string address = "";
            FileInfo file = new FileInfo(hlpPath);
            address =File.ReadAllText(hlpPath).Trim().ToLower();
            //帮助文档地址不是http开头，则查找hosturl
            if (!address.StartsWith("http"))
            {
                string host = GetHostURL(file.DirectoryName);
                if (!host.EndsWith("/") && !address.StartsWith("/"))
                {
                    address = host + "/" + address;
                }
                else
                {
                    address = host + address;
                }
            }
            System.Diagnostics.Process.Start(address);
        }

        /// <summary>
        /// 一级级向上查找HostURL地址
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        /// 创建者：杜一博
        /// 创建时间： 2014年5月14日
        static string GetHostURL(string dirPath)
        {
            string[] filePaths = Directory.GetFiles(dirPath);
            foreach (string fPath in filePaths)
            {
                if (new FileInfo(fPath).Name.ToLower() == HOST_URL)
                {
                    return File.ReadAllText(fPath).Trim();
                }
            }
            //如果已经到了根目录文件夹，则不要再往上找了
            if (new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName!=dirPath)
            {
                return GetHostURL(new DirectoryInfo(dirPath).Parent.FullName);
            }
            return "";
        }
    }
}
